package com.leetcode.contest;

import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2021/11/13 22:35
 */
public class LC5911模拟行走机器人2 {

    public static void main(String[] args) {
        Robot robot = new Robot(6, 3);
        for (int i = 0; i < 14; i++) {
            robot.move(2);
            System.out.println(Arrays.toString(robot.getPos()));
            System.out.println(robot.getDir());
        }
//        robot.move(2);
//        robot.move(2);
//        System.out.println(Arrays.toString(robot.getPos()));
//        System.out.println(robot.getDir());
//        robot.move(2);
//        robot.move(1);
//        robot.move(4);
//        System.out.println(Arrays.toString(robot.getPos()));
//        System.out.println(robot.getDir());
    }

    private static class Robot {

        private final String[] dir = {"East", "North", "West", "South"};
        private final int[][] grid;
        private final int[] cur = new int[2];
        private int curDirIndex = 0;

        public Robot(int width, int height) {
            grid = new int[height][width];
            cur[0] = height - 1;
            cur[1] = 0;
        }

        public void move(int num) {
            for (int count = 0; count < num; count++) {
                switch (curDirIndex) {
                    case 0:
                        //东
                        cur[1] += 1;
                        if (cur[1] == grid[0].length - 1) curDirIndex = (++curDirIndex) % 4;
                        break;
                    case 1:
                        //北
                        cur[0] -= 1;
                        if (cur[0] == 0) curDirIndex = (++curDirIndex) % 4;
                        break;
                    case 2:
                        //西
                        cur[1] -= 1;
                        if (cur[1] == 0) curDirIndex = (++curDirIndex) % 4;
                        break;
                    case 3:
                        //南
                        cur[0] += 1;
                        if (cur[0] == grid.length - 1) curDirIndex = (++curDirIndex) % 4;
                        break;
                }
            }
        }

        public int[] getPos() {
            return new int[]{cur[1], grid.length - cur[0] - 1};
        }

        public String getDir() {
            return dir[curDirIndex];
        }
    }
}
